﻿using AutoMapper;
using Easy.Common.Results;
using Easy.Services.Dtos;
using Easy.Services.Entities;
using Easy.Services.Interface;
using Microsoft.AspNetCore.Mvc;

namespace Easy.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PermissionController(IPermissionService _permissionService, IMapper _mapper) : BaseController
    {
        readonly IPermissionService permissionService = _permissionService;
        readonly IMapper mapper = _mapper;

        [HttpPost]
        public APIResult Add(PermissionDTO dto)
        {
            APIResult result = new();
            if (permissionService.Exist(p => p.Code.Equals(dto.Code) && p.IsDelete == false && p.Type == dto.Type))
            {
                result.Message = "编码已存在";
                return result;
            }
            var model = mapper.Map<Permission>(dto);
            model.SetModelBase(tokenModel: TokenModel, isInit: true);
            var permission = permissionService.Add(model);
            if (permission.Id > 0)
            {
                result.Data = permission;
                result.Success = true;
                result.Message = "添加成功";
            }
            else
            {
                result.Message = "添加失败";
            }
            return result;

        }


        [HttpGet]
        public APIResult Get(long id)
        {
            APIResult result = new();
            var model = permissionService.Get(p => p.Id == id);
            if (model != null)
            {
                result.Success = true;
                result.Data = model;
            }
            return result;
        }

        [HttpPost]
        public APIResult Update(UpdatePermissionDTO dto)
        {
            APIResult result = new();
            var model = mapper.Map<Permission>(dto);
            model.SetModelBase(tokenModel: TokenModel);
            var res = permissionService.Update(model);
            result.Success = true;
            result.Data = res;
            return result;

        }

        [HttpDelete]
        public APIResult Delete(long id)
        {
            APIResult result = new();
            if (id > 0 && id < 9)
            {
                result.Message = "系统保留不允许删除";
                return result;
            }
            var model = permissionService.Get(p => p.Id == id);
            if (model == null)
            {
                result.Message = "数据不存在";
                return result;
            }
            model.SetModelBase(tokenModel: TokenModel);
            var res = permissionService.Delete(model);
            result.Success = res;
            result.Message = res == true ? "删除成功" : "删除失败";
            return result;

        }


        [HttpGet]
        public APIResult GetList()
        {
            APIResult result = new();
            var list = permissionService.GetList();
            result.Success = true;
            result.Data = list;
            return result;

        }


    }
}
